package com.bjtu.ptsite.utils;

public class RequestUtil {

    private static final ThreadLocal<UserContext> userContextHolder = new ThreadLocal<>();

    public static void setUserContext(int userId, int identificationNumber) {
        UserContext context = new UserContext();
        context.setIdentificationNumber(identificationNumber);
        context.setId(userId);
        userContextHolder.set(context);
    }

    public static void setId(int id) {
        UserContext context = new UserContext();
        context.setId(id);
        userContextHolder.set(context);
    }

    public static UserContext getUserContext() {
        return userContextHolder.get();
    }

    public static Integer getIdentificationNumber() {
        UserContext context = userContextHolder.get();
        return context != null ? context.getIdentificationNumber() : null;
    }

    public static void clear() {
        userContextHolder.remove();
    }

    // 内部类：可扩展用户信息
    public static class UserContext {
        private int identificationNumber;
        private int id;

        public int getIdentificationNumber() {
            return identificationNumber;
        }

        public int getId() {
            return id;
        }


        public void setIdentificationNumber(int identificationNumber) {
            this.identificationNumber = identificationNumber;
        }
        public void setId(int id) {
            this.id = id;
        }
    }
}
